package immutablecollections.textboxes;

import immutablecollections.misc.*;
import java.util.*;


public class LeafTextBox extends TextBox
{
     final private  List<String> lines;

     public LeafTextBox(int width, int height, String... strings)
     {
          super(width, height);
          this.lines = new ArrayList<String>(strings.length);

          int count = 0;
          for (String string : strings)
          {
               count++;
               if (count > height)
                    break;
               lines.add(string);
          }
     }

     public String getLine(int index)
     {
          if (index > lines.size())
               return TextUtils.repeat(" ", width);
          else
          {
               String line = lines.get(index - 1);
               if (line.length() < width)
                    return line + TextUtils.repeat(" ", width - line.length());
               else
                    return line.substring(0, width);
          }
     }

     public static LeafTextBox centred(String text, int widthToCentreIn)
     {
          int lead = Math.max(0, (widthToCentreIn - text.length()) / 2);
          return new LeafTextBox(widthToCentreIn, 1, TextUtils.repeat(" ", lead) + text);
     }

     public static LeafTextBox righted(String text, int width)
     {
          int lead = Math.max(0, width - text.length());
          return new LeafTextBox(width, 1, TextUtils.repeat(" ", lead) + text);
     }

     public static LeafTextBox withMargin(String text, int marginSize)
     {
          String margin = TextUtils.repeat(" ", marginSize);
          return new LeafTextBox(text.length() + marginSize * 2, 1, margin + text);
     }

     public static TextBox with(String text)
     {
          return new LeafTextBox(text.length(), 1, text);
     }

}
